#!/bin/bash

# 配置文件路径
CONFIG_FILE="/etc/packet_capture.ini"
# 进程名称
cmd_process="packet_capture"

# 检查配置文件是否存在
if [ ! -f "$CONFIG_FILE" ]; then
    echo "Config file $CONFIG_FILE which does not exist。"
    exit 1
fi

# 从配置文件中读取网卡信息和 VLAN ID 范围
INTERFACE=$(grep "INTERFACE:" "$CONFIG_FILE" | cut -d: -f2 | tr -d ' ')
START_VLAN=$(grep "START_VLAN:" "$CONFIG_FILE" | cut -d: -f2 | tr -d ' ')
END_VLAN=$(grep "END_VLAN:" "$CONFIG_FILE" | cut -d: -f2 | tr -d ' ')
SAVE_DIR=$(grep "SAVE_PATH:" "$CONFIG_FILE" | cut -d: -f2 | tr -d ' ')

#MQTT
MQTT_HOST=$(grep "MQTT_HOST:" "$CONFIG_FILE" | cut -d: -f2 | tr -d ' ')
MQTT_PORT=$(grep "MQTT_PORT:" "$CONFIG_FILE" | cut -d: -f2 | tr -d ' ')
USER_NAME=$(grep "MQTT_USERNAME:" "$CONFIG_FILE" | cut -d: -f2 | tr -d ' ')
USER_PWD=$(grep "MQTT_USERPWD:" "$CONFIG_FILE" | cut -d: -f2 | tr -d ' ')
MQTT_KEEPALIVE=$(grep "keepalive:" "$CONFIG_FILE" | cut -d: -f2 | tr -d ' ')
MQTT_QOS=$(grep "qos:" "$CONFIG_FILE" | cut -d: -f2 | tr -d ' ')
MQTT_SUBQOS=$(grep "subqos:" "$CONFIG_FILE" | cut -d: -f2 | tr -d ' ')
MQTT_ID=$(grep "id:" "$CONFIG_FILE" | cut -d: -f2 | tr -d ' ')
MQTT_CAFILE=$(grep "cafile:" "$CONFIG_FILE" | cut -d: -f2 | tr -d ' ')
MQTT_CART=$(grep "cert:" "$CONFIG_FILE" | cut -d: -f2 | tr -d ' ')
MQTT_KEY=$(grep "key:" "$CONFIG_FILE" | cut -d: -f2 | tr -d ' ')
MQTT_KEYPWD=$(grep "keypassword:" "$CONFIG_FILE" | cut -d: -f2 | tr -d ' ')
MQTT_W_PAYLOAD=$(grep "willpayload:" "$CONFIG_FILE" | cut -d: -f2 | tr -d ' ')
MQTT_W_QOS=$(grep "willqos:" "$CONFIG_FILE" | cut -d: -f2 | tr -d ' ')
MQTT_W_RETAIN=$(grep "willretain:" "$CONFIG_FILE" | cut -d: -f2 | tr -d ' ')
MQTT_W_TOPIC=$(grep "willtopic:" "$CONFIG_FILE" | cut -d: -f2 | tr -d ' ')

if [ -z "$INTERFACE" ] || [ -z "$START_VLAN" ] || [ -z "$END_VLAN" ]; then
    echo "Can not read VLAN ID or other option in config file, please check!"
    echo "config file:$CONFIG_FILE"
    echo "eg:  interface: ens33"
    echo "    start_vlan: 4000"
    echo "      end_vlan: 4001"
    exit 1
fi
if [ -n "$MQTT_HOST" ]; then
    MQTT_HOST=" --host=${MQTT_HOST}"
fi
if [ -n "$MQTT_PORT" ]; then
    MQTT_PORT=" --port=${MQTT_PORT}"
fi
if [ -n "$USER_NAME" ]; then
    USER_NAME=" --username=${USER_NAME}"
fi
if [ -n "$USER_PWD" ]; then
    USER_PWD=" --password=${USER_PWD}"
fi
if [ -n "$MQTT_KEEPALIVE" ]; then
    MQTT_KEEPALIVE=" --keepalive=${MQTT_KEEPALIVE}"
fi
if [ -n "$MQTT_QOS" ]; then
    MQTT_QOS=" --qos=${MQTT_QOS}"
fi
if [ -n "$MQTT_SUBQOS" ]; then
    MQTT_SUBQOS=" --subqos=${MQTT_SUBQOS}"
fi
if [ -n "$MQTT_ID" ]; then
    MQTT_ID=" --id=${MQTT_ID}"
fi
if [ -n "$MQTT_CAFILE" ]; then
    MQTT_CAFILE=" --cafile=${MQTT_CAFILE}"
fi
if [ -n "$MQTT_CART" ]; then
    MQTT_CART=" --cert=${MQTT_CART}"
fi
if [ -n "$MQTT_KEY" ]; then
    MQTT_KEY=" --key=${MQTT_KEY}"
fi
if [ -n "$MQTT_KEYPWD" ]; then
    MQTT_KEYPWD=" --keypassword=${MQTT_KEYPWD}"
fi
if [ -n "$MQTT_W_PAYLOAD" ]; then
    MQTT_W_PAYLOAD=" --willpayload=${MQTT_W_PAYLOAD}"
fi
if [ -n "$MQTT_W_QOS" ]; then
    MQTT_W_QOS=" --willqos=${MQTT_W_QOS}"
fi
if [ -n "$MQTT_W_RETAIN" ]; then
    MQTT_W_RETAIN=" --willretain=${MQTT_W_RETAIN}"
fi
if [ -n "$MQTT_W_TOPIC" ]; then
    MQTT_W_TOPIC=" --willtopic=${MQTT_W_TOPIC}"
fi
if [ -z "$SAVE_DIR" ]; then
    SAVE_DIR=" --pcap_path=~"
else
    SAVE_DIR=" --pcap_path=${SAVE_DIR}"
fi

# 检查并启动程序
check_and_start_program() {
    local vlan_id=$1
    local process_name="${cmd_process}_${VLAN_INTERFACE}"

    local process_count=$(ps -ef | grep "$change_name"|grep "$VLAN_INTERFACE" | grep -v grep | wc -l)

    if [ $process_count -eq 0 ]; then
        #echo "run process:$cmd_process --process_name=$process_name --pcap_dev=$VLAN_INTERFACE --vlanid=$vlan ${MQTT_HOST} ${MQTT_PORT} --topic="/pkgcap/$vlan/ToManager" --subtopic="/pkgcap/$vlan/ToCapture" ${USER_NAME} ${USER_PWD} ${MQTT_KEEPALIVE} ${MQTT_QOS} ${MQTT_SUBQOS} ${MQTT_ID} ${MQTT_CAFILE} ${MQTT_CART} ${MQTT_KEY} ${MQTT_KEYPWD} ${MQTT_W_PAYLOAD} ${MQTT_W_QOS} ${MQTT_W_RETAIN} ${MQTT_W_TOPIC} --daemon"
        echo "Start process:[$process_name]"
        $cmd_process --process_name=${process_name} --pcap_dev=${VLAN_INTERFACE} --vlanid=$vlan ${MQTT_HOST} ${MQTT_PORT} --topic="/pkgcap/$vlan/ToManager" --subtopic="/pkgcap/$vlan/ToCapture" ${USER_NAME} ${USER_PWD} ${MQTT_KEEPALIVE} ${MQTT_QOS} ${MQTT_SUBQOS} ${MQTT_ID} ${MQTT_CAFILE} ${MQTT_CART} ${MQTT_KEY} ${MQTT_KEYPWD} ${MQTT_W_PAYLOAD} ${MQTT_W_QOS} ${MQTT_W_RETAIN} ${MQTT_W_TOPIC} ${SAVE_DIR} --daemon
    fi
}
# 检查并停止程序
check_and_stop_program() {
    local vlan_id=$1
    local process_name="${cmd_process}_${VLAN_INTERFACE}"
    local process_pid="$(pgrep -d ' ' -f "$process_name")"

    if [ -n "$process_pid" ]; then

        kill -9 $process_pid
        echo "Stop process [${process_name}(pid:${process_pid})])"
    fi
}

start() {
    # 循环创建并启动 VLAN 接口
    ip link set $INTERFACE up
    sleep 1
    for ((vlan=$START_VLAN; vlan<=$END_VLAN; vlan++)); do
        VLAN_INTERFACE="${INTERFACE}.${vlan}"
        #检查 VLAN 借口是否存在
        if ip link show dev $VLAN_INTERFACE >/dev/null 2>&1; then
            echo "VLAN  $VLAN_INTERFACE dose exist"
            ip link set dev $VLAN_INTERFACE up
        else
            # 创建 VLAN 接口
            ip link add link $INTERFACE name $VLAN_INTERFACE type vlan id $vlan
            if [ $? -eq 0 ]; then
                echo "Creat VLAN $VLAN_INTERFACE success!"
                # 启动 VLAN 接口
                ip link set dev $VLAN_INTERFACE up
                if [ $? -eq 0 ]; then
                    echo "Set $VLAN_INTERFACE UP success!"
                else
                    echo "Set $VLAN_INTERFACE UP failed!."
                fi
            else
                echo "Creat VLAN $VLAN_INTERFACE failed!"
            fi
        fi
        check_and_start_program $vlan
    done
}

stop () {
    for ((vlan=$START_VLAN; vlan<=$END_VLAN; vlan++)); do
        VLAN_INTERFACE="${INTERFACE}.${vlan}"
        check_and_stop_program $vlan
        #检查 VLAN 借口是否存在
        if ip link show dev $VLAN_INTERFACE >/dev/null 2>&1; then
            echo "VLAN  $VLAN_INTERFACE dose exist, del it."
            ip link set $VLAN_INTERFACE down
            ip link del $VLAN_INTERFACE
        fi
    done
}

cmd="$1"
if [ "$cmd" == "start" ]; then
    start
elif [ "$cmd" == "stop" ]; then
    stop
else
    echo "Usage: $0 {start|stop}"
    echo ""
fi
